package com.goldgov.mobile;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.gtiles.utils.HttpServletRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.auditlog.service.AuditLog;
import com.goldgov.auditlog.service.IAuditLogService;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/mobile/auditLog")
@Api("审计日志接口")
public class AuditLogForMobileController {

	

	@Autowired
	private IAuditLogService auditLogService;
	
	
	/**
	 * 新增审计日志
	 * @param auditLog 
	 * @param model
	 * @param request
	 * @return
	 */
	@PostMapping("/addAuditLog")
	@ApiOperation(value = "新增审计日志", notes = "新增审计日志" )
	public JsonObject<Object> addInfo( AuditLog auditLog,HttpServletRequest request){
		LoginAuthUser user=LoginAuthUser.getUser(request);
		//LoginAuthUser user=LoginAuthUser.getUserTest(request);
		auditLog.setOperateTime(new Date());
		auditLog.setOperateType("Find");//方式为查询
		auditLog.setOperateUserId(user.getEntityID());
		auditLog.setOperateUserName(user.getUserName());
		auditLog.setOperateIp(getIpAddress(request));//获取用户真实ip
		auditLog.setOperateName("App访问"+auditLog.getOperateName());
		auditLogService.addAuditLog(auditLog);
		return JsonSuccessObject.SUCCESS;
	}
	
	
	
	/** 
     * 获取用户真实IP地址，不使用request.getRemoteAddr()的原因是有可能用户使用了代理软件方式避免真实IP地址, 
     * 可是，如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值 
     *  
     * @return ip
     */
    @SuppressWarnings("unused")
	private String getIpAddress(HttpServletRequest request) {
    	String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if( ip.indexOf(",")!=-1 ){
    		ip = ip.split(",")[0];
    	}
		return ip;
    }
	
	
//	private  String getIpAddress(HttpServletRequest request) {
//		String ip = request.getHeader("x-forwarded-for");
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//			ip = request.getHeader("Proxy-Client-IP");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//			ip = request.getHeader("WL-Proxy-Client-IP");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//			ip = request.getHeader("HTTP_CLIENT_IP");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//			ip = request.getRemoteAddr();
//		}
//		return ip;
//	}


}
